package com.dataStructure.线性表.数组;

import com.dataStructure.线性表.list_父类.AbstractList;

@SuppressWarnings("all")
public class ArrayList_缩容<E> extends AbstractList<E> {
    //所有的元素
    private E[] elements;
    //初始容量
    private static final int DEFAULT_CAPAITY = 10;

    public ArrayList_缩容(int capaticy) {
        capaticy = (capaticy > DEFAULT_CAPAITY) ? capaticy : DEFAULT_CAPAITY;
        elements = (E[]) new Object[capaticy];
    }

    public ArrayList_缩容() {
        this(DEFAULT_CAPAITY);
    }


    //获取传入索引处的值
    public E get(int index) {
        rangeCheck(index);
        return elements[index];
    }

    //往索引处设置值（覆盖原值）
    public E set(int index, E element) {
        rangeCheck(index);
        E old = elements[index];
        elements[index] = element;
        return old;
    }

    //添加元素，往索引处插入一个值
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapaity(size + 1);

        for (int i = size - 1; i >= index; i--) {
            elements[i + 1] = elements[i];
        }
        elements[index] = element;
        size++;
    }

    //删除索引处的值
    public E remove(int index) {
        rangeCheck(index);
        E old = elements[index];
        for (int i = index + 1; i < size; i++) {
            elements[i - 1] = elements[i];
        }
        elements[--size] = null;
        return old;
    }

    //删除元素
    public E remove(E element) {
        return remove(indexOf(element));
    }

    //查看元素的索引
    public int indexOf(E element) {
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (elements[i] == null) return i;
            }
        }

        for (int i = 0; i < size; i++) {
            if (element.equals(elements[i])) return i;
        }
        return ELEMENT_NOT_FOUND;
    }


    //扩容
    private void ensureCapaity(int capaity) {
        int oldCapaity = elements.length;
        if (oldCapaity >= capaity) return;
        //扩容为原数组的1.5倍
        int newCapaity = oldCapaity + (oldCapaity >> 1);
        E[] newElements = (E[]) new Object[newCapaity];
        for (int i = 0; i < size - 1; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println("beforekuorong:" + oldCapaity + "afterkuorong" + newCapaity);
    }

    //清除所有元素
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        this.size = 0;
        if (elements != null && elements.length > DEFAULT_CAPAITY) {
            elements = (E[]) new Object[DEFAULT_CAPAITY];
        }
    }

    private void trim() {
        int capacity = elements.length;
        int newCapacity = capacity >> 1;
        if (size >= (newCapacity) || capacity <= DEFAULT_CAPAITY) return;
        //剩余空间很多
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(",[");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append((elements[i]));
          /*  if (i != size -1){
                string.append(",");
            }*/
        }
        string.append("]");
        return string.toString();
    }


}
